using NHibernate;
using NHibernate.Tool.hbm2ddl;

namespace SmartHRMS.NhibernateRepository
{
    public class SessionFactory
    {
        private static ISessionFactory _SessionFactory;
		
        private static void Init()
        {
            NHibernate.Cfg.Configuration config;
			
            config = new NHibernate.Cfg.Configuration();
            config.AddAssembly("SmartHRMS.NhibernateRepository");
			
            log4net.Config.XmlConfigurator.Configure();
			
            // As long as we have an empty database with the same name as the one in our
            // connection string Nhibernate will generate the schema for us, we only want
            // to run this once.
            //SchemaExport se = new SchemaExport(config);
            //se.Execute(false, true, false, false);

            config.Configure();
			
            _SessionFactory = config.BuildSessionFactory();
        }
		
        public static ISessionFactory GetSessionFactory()
        {
            if (_SessionFactory == null)
            {
                Init();
            }
			
            return _SessionFactory;
        }
		
        public static ISession GetNewSession()
        {
            return GetSessionFactory().OpenSession();
        }
		
        public static ISession GetCurrentSession()
        {
            return GetSessionFactory().GetCurrentSession();
        }
    }
}
